<?php

class User {
// variabili
    public $id;
    public $nome;
    public $cognome;
    public $mail;
    public $username;
    public $password;
    public $telefono;
    public $cap;
    public $citta;
    public $eta;
    public $indirizzo;
    public $immagine;
    public $id_tipo_user;
    public $id_topic;

    public function __construct() {

    }

    public function getMyID() {
        return $this->id;
    }
    public function getUsername() {
        return $this->username;
    }

    public  static function registra_utente($nome,$cognome,$mail,$username,$password,$telefono,$cap,$citta,$eta,$indirizzo) {

        $password = md5($password);

        $query = "INSERT INTO users
            (nome, cognome, mail, username, password, telefono, cap, citta, data_nascita, indirizzo, id_tipo_user)
            VALUES ('$nome','$cognome','$mail','$username','$password','$telefono','$cap','$citta','$eta','$indirizzo',1)";

        //return $query;
        mysql_query($query) or die($query);
        echo "<script> alert('Registrato correttamente') </script>";
        return true;
    }

    public  static function modifica_utente($id,$nome,$cognome,$username,$password,$nuova) {
        $queryy = mysql_query("select password from users where id = ".$id.";");
        $row = mysql_fetch_array($queryy);
        
        if(md5($_POST['oldPass']) == $row['password']){
        $query =    "UPDATE users
                      SET nome='$nome'
                         ,cognome='$cognome'
                         ,username='$username'
                         ,password='".md5($nuova)."'
                        WHERE id=$id";
        mysql_query($query);
        
        }

        else{echo "error";}




        //return $query;
        mysql_query($query) or die($query);
//        echo "<script> alert('Utente Modificato') </script>";
        return true;
    }



    public function login_utente($username,$password) {

        $user = $_POST["username"];
        $pass = md5($_POST["password"]);

        $query = "select * from users where password = '$pass' and username = '$user'";
        $ris = mysql_query($query);

        $row = mysql_fetch_assoc($ris);

        if($row['username'] == $user & $row['password'] == $pass) {

            $this->id = $row["id"];
            $this->nome = $row["nome"];
            $this->cognome = $row["cognome"];
            $this->mail = $row["mail"];
            $this->username = $row["username"];
            $this->immagine = $row["immagine"];
            $this->id_tipo_user = $row["id_tipo_user"];
            $this->setSession();

            $data = date('d.m.y', time());
            $sql = "INSERT into tablog(data,descrizione,id_utente)
                        VALUES('".$data."','LOGIN', '".$this->id."') ";
            $ris = mysql_query($sql);

            if($row['username'] == "admin")
                Util::redirectAdmin();
            else
                Util::redirect();


        }

        else
            echo "<script> alert('Errore nel login !') </script>";




    }
    function setSession() {
        $_SESSION['user'] = serialize($this);
    }
    public static function logout() {
        session_destroy();
    }
    public function aggiornaLog() {


    }
    // metodi
    public function verifica_utente($username, $password) {
        if($username == "luca" && $password == "luca") {
            $User = new User("Luca","Margonar", "", $username, "");
            $_SESSION["user"] = serialize($User);
            return true;
        }
        else {
            return false;
        }
    }

    public function verifica_mail($mail) {
        if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $mail)) {
            echo "Email valido";
        }
        else {
            echo "Email non valido";
        }
    }

    public function get_nome_completo() {
        return $this->nome . " " . $this->cognome;
    }

    public function change_image($link) {
        $query =  "UPDATE users set immagine = '" . $link . "'  where id = " . $this->id;
        mysql_query($query);
//        echo $query;
        $this->immagine = $link;
//        $this->setSession();
    }

    public function inserisci_ticket($nome, $mail, $descrizione, $id, $oggi, $topic) {
    //Funzione per inserire i ticket
        $ticket = "INSERT into ticket
                (id, nome, data, descrizione, id_users, mail, id_topic)
                VALUES (null, '$nome','$oggi','$descrizione' , $id,'$mail' ,$topic)";

        mysql_query($ticket);
        return true;
    }

    public function nomeCognome() {

        return $this->nome . ' ' . $this->cognome;
    }

    public function assegna_ticket($id_ticket, $id_user) {
    //Funzione per assegnare un ticket ad un collaboratore
    //Prima si registra il ticket nella tabella assegnato e poi si aggiorna lo stato del ticket
    //$oggi = date('d.m.y hh:mm:ss', time());
    //Stampare la data nel formato corretto
        $oggi = date("y-m-d H:i:s",time());

        $ticket = "INSERT into assegnato
                (id_ticket, id_user, data)
                VALUES (" . $id_ticket . "," . $id_user.",'". $oggi ."')";
        $update = "UPDATE ticket SET
                id_stato=2
                WHERE id=".$id_ticket;
        //echo $ticket;
        //echo $update;

        mysql_query($ticket);
        mysql_query($update);
        return true;
    }
    public static function assegnaRuolo($id_user,$id_ruolo) {
        $update = "UPDATE users SET
                id_tipo_user = '".$id_ruolo."'
                WHERE id='".$id_user."'";

        mysql_query($update);
        return true;
    }


}
?>